//
//  UIImage+Util.m
//  CarUser
//
//  Created by ayi on 2018/12/24.
//  Copyright © 2018 王森林. All rights reserved.
//

#import "UIImage+Util.h"

@implementation UIImage (Util)

+ (UIImage *)getDefaultImage{
    return  [UIImage imageNamed:@"default_bgimg"];
}

+ (UIImage *)getDefaultPersonLogoImage{
    return IMG(@"ic_info_head");
}

+ (UIImage *)AppiconImage{
    NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    return [UIImage imageNamed:icon];
}



//压缩图片至100k以下
+ (NSData *)imageDataTo100kb:(UIImage *)image
{
    NSData *data=UIImageJPEGRepresentation(image, 1.0);
    if (data.length>100*1024) {
        if (data.length>1024*1024) {//1M以及以上
            data=UIImageJPEGRepresentation(image, 0.1);
        }else if (data.length>512*1024) {//0.5M-1M
            data=UIImageJPEGRepresentation(image, 0.5);
        }else if (data.length>200*1024) {//0.25M-0.5M
            data=UIImageJPEGRepresentation(image, 0.9);
        }
    }
    return data;
}

/**
 *  压缩图片分辨率
 *
 *  @param originalImage 原图
 *  @param maxWidth      最大宽度
 *  @param maxHeight     最大高度
 *
 *  @return 压缩分辨率后的图片
 */
+ (UIImage *)imageCompressToJPGWithImgage:(UIImage *)originalImage
                                 maxWidth:(NSInteger)maxWidth
                                maxHeight:(NSInteger)maxHeight {
    
    CGSize size = [self conversionSizeWithSize:originalImage.size maxWidth:maxWidth maxHeight:maxHeight];
    
    if (CGSizeEqualToSize(originalImage.size, size)) {
        // 分辨率没有改变直接返回原图
        return originalImage;
    }
    
    // Scale the original image to match the new size.
    UIGraphicsBeginImageContext(size);
    [originalImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* compressedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return compressedImage;
}

// 根据宽高计算压缩后的分辨率，宽高同时不超过设置的最大值
+ (CGSize)conversionSizeWithSize:(CGSize)size maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight {
    float scaleWidth = 1;
    float scaleHeight = 1;
    if (size.width > maxWidth) {
        scaleWidth = (float)maxWidth / (float)size.width;
    }
    if (size.height > maxHeight) {
        scaleHeight = (float)maxHeight / (float)size.height;
    }
    
    float scale = MIN(scaleHeight, scaleWidth);
    CGSize conversionSize = CGSizeMake(size.width * scale, size.height * scale);
    
    return conversionSize;
}


- (UIImage*)resizeImage:(UIImage*)image toWidth:(NSInteger)width height:(NSInteger)height
{
    // Create a graphics context with the target size
    CGSize size = CGSizeMake(width, height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Flip the context because UIKit coordinate system is upside down to Quartz coordinate system
    CGContextTranslateCTM(context, 0.0, height);
    CGContextScaleCTM(context, 1.0, -1.0);
    // Draw the original image to the context
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), image.CGImage);
    // Retrieve the UIImage from the current context
    UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageOut;
}

@end
